﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace TMIS
{
    public partial class frmThesis : XtraForm
    {
        private BLL.Thesis bll = new BLL.Thesis();
        public frmThesis()
        {
            InitializeComponent();
            this.FillComponent();
        }
        private void FillComponent()
        {
            comTitleSource.Properties.Items.AddRange(bll.GetTSID());
            comInstruction.Properties.Items.AddRange(bll.GetTeacher());
            comReview.Properties.Items.AddRange(bll.GetTeacher());
            this.thesisTableAdapter.Fill(this.reportDS.Thesis);
        }
        private void btnHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            XtraMessageBox.Show("结合实际解决生产单位实际工程问题的填写“A”;\n来自于生产实际已解决了的问题，重新让学生进行模拟的填写“B”;\n来自科研项目的全部或部分研究内容,是结合科研任务进行的填写“C”;\n其他填写“D”");
        }

        private void btnAddThesis_Click(object sender, EventArgs e)
        {
            if (bll.Search(txtTitle.Text))
            {
                Model.Thesis thesis = new Model.Thesis()
                {
                    ID = Guid.NewGuid(),
                    Title = txtTitle.Text,
                    TitleSourceID = bll.GetTSID(comTitleSource.Text),
                    InstructionTeacherID = bll.GetTeacherID(comInstruction.Text),
                    ReviewTeacherID = bll.GetTeacherID(comReview.Text),
                    FundsSource = txtFundScource.Text
                };
                try
                {
                    bll.Insert(thesis);
                    XtraMessageBox.Show("论文信息添加成功！");
                    this.thesisTableAdapter.Fill(this.reportDS.Thesis);
                }
                catch (Exception error)
                {
                    #if DEBUG
                    XtraMessageBox.Show(error.Message);
                    #else
                    XtraMessageBox.Show("论文信息添加失败！请检查所有信息是否填写完整！");
                    #endif
                }
            }
            else
            {
                XtraMessageBox.Show("论文题目重复，论文信息添加失败！");
            }
        }

        private void frmThesis_Load(object sender, EventArgs e)
        {

        }

        private void btnDelThesis_Click(object sender, EventArgs e)
        {
            try
            {
                //Guid guid = new Guid(this.TeacherView.SelectedRows[0].Cells[5].Value.ToString());
                Guid guid = new Guid(this.dataGridView.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString());
                bll.Del(guid);
                XtraMessageBox.Show("论文信息删除成功！");
                this.thesisTableAdapter.Fill(this.reportDS.Thesis);
            }
            catch (SqlException error)
            {
                #if DEBUG
                XtraMessageBox.Show(error.Message);
                #else
                XtraMessageBox.Show("该论文已被学生选为论文题目，若要继续，请先更改学生选题信息！");
                #endif
            }
        }
    }
}
